#include "gpio_dragonball.h"

namespace igslib
{
    gpio_Grgn::gpio_Grgn(char id)
    {
        ID=id;
        m01spi = nullptr;
        number=0; //default
    }
    gpio_Grgn::gpio_Grgn()
    {
        m01spi = nullptr;
        number=0; //default
    }
    void gpio_Grgn::SetSPI(ISpi* value) { m01spi = value; }
    int gpio_Grgn::SetNumber(const int& value)
    { 			
        int rtn = 0;
        if(value>=0 && value<15)
        {
            number = value;
        }
        else
        {
            util::Log("GPIO Index of MH_board is cross boundary");		
            rtn = -1;
        }
        return rtn;
    }
    int gpio_Grgn::SetState(const state& lv)
    {
        Protocol_DragonBall* tmp = (Protocol_DragonBall*)(m01spi);
        if(lv==state::low)
            tmp->SetGPout(number,0);
        else if(lv==state::high)
            tmp->SetGPout(number,1);
        st=lv;		
        return 0;
    }
    IGPIO::state gpio_Grgn::GetState()
    {
        char dtmp[16];
        int pos=(number)/8;
        int offset=(number)%8;

        Protocol_DragonBall* tmp = (Protocol_DragonBall*)(m01spi);
        int len=tmp->GetGPin(dtmp);
        if(number<8)
        {
            if(dtmp[pos]&(0x1<<offset))
                st=state::high;
            else
                st=state::low;
        }	
        return st;
    }
};
